//
//  AllNewCityDynamicVC.m
//  CenturyCity
//
//  Created by beisheng03 on 2017/12/14.
//  Copyright © 2017年 beisheng. All rights reserved.
//

#import "AllNewCityDynamicVC.h"
#import "NewCityHomeCell.h"
#import "MyWebViewController.h"
@interface AllNewCityDynamicVC ()

@property (nonatomic, assign) NSInteger curpage;

@property (nonatomic, strong) NSString *hasmore;


/** <#note#> */
@property (nonatomic, strong) NSMutableArray *dynamicModels;

@end



@implementation AllNewCityDynamicVC



- (NSMutableArray *)dynamicModels{
    
    if (!_dynamicModels) {
        _dynamicModels = [NSMutableArray array];
        
        NewCityDynamicModel *model1 = [[NewCityDynamicModel alloc]init];
        model1.dynamicTitle = @"襄阳初雪: 你在老城区里寸步难行,我在浩然河畔美出诗意!";
        model1.dynamicTime = @"2016-11-25 18:52:00";
        model1.dynamicImage = @"http://mmbiz.qpic.cn/mmbiz_png/1YYHSkniajiaH1d0WxzpbwGfkPpu7x5dDw6H8K3rwFibZfy6qdibfPXfp9ibW6TDgSeSJQUgYMeqhiaw1krYchDLKZPQ/640?wx_fmt=png&wxfrom=5&wx_lazy=1";
        model1.dynamicDetailUrl = @"http://mp.weixin.qq.com/s/nn3hwmmLftYfvGoW1ibMFA";
        
        NewCityDynamicModel *model2 = [[NewCityDynamicModel alloc]init];
        model2.dynamicTitle = @"东津新区最新美图来袭，你关注的学校、医院、市民中心都在这，拿走不谢！";
        model2.dynamicTime = @"2017-6-27 18:07:00";
        model2.dynamicImage = @"https://mmbiz.qpic.cn/mmbiz_jpg/1YYHSkniajiaFcgrX2S7IWySTELFedbp7xlwVFEibf7DrGzf836m15Fy5NSsB8kms2pdR260HhS0s4LBLia2gydHaw/640?wx_fmt=jpeg&wxfrom=5&wx_lazy=1";
        model2.dynamicDetailUrl = @"http://mp.weixin.qq.com/s/Gx4ZTSNrNsbQNYs4AzYLJg";
        
        NewCityDynamicModel *model3 = [[NewCityDynamicModel alloc]init];
        model3.dynamicTitle = @"市科技馆主体即将完工,'探索之眼'造型抢先看!!!";
        model3.dynamicTime = @"2016-11-18 18:07:00";
        model3.dynamicImage = @"http://mmbiz.qpic.cn/mmbiz_jpg/1YYHSkniajiaHyVwvISLNGu2Tw1fpp93x04aHGk2U8iasj7vZ3F9GibXARicELDchaoynO9hfjYPhPDk2cgDLUQLEyA/640?wx_fmt=jpeg&wxfrom=5&wx_lazy=1";
        model3.dynamicDetailUrl = @"http://mp.weixin.qq.com/s/Q4fStCYW8jjjrs0oK8GmAQ";
        
        [_dynamicModels addObject:model2];
        [_dynamicModels addObject:model1];
        [_dynamicModels addObject:model3];
        
    }
    
    
    
    return _dynamicModels;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"新城动态";
    
    if (@available(iOS 11.0, *)) {
        self.navigationItem.largeTitleDisplayMode =  UINavigationItemLargeTitleDisplayModeNever;
    }
    
    self.plainTableView.tableFooterView = [[UIView alloc]init];
    
    self.plainTableView.backgroundColor = [UIColor whiteColor];
    
    self.plainTableView.rowHeight = UITableViewAutomaticDimension;
    
    self.plainTableView.estimatedRowHeight = 100;
    
    
    [self.plainTableView registerNib:[UINib nibWithNibName:NSStringFromClass([NewCityHomeCell class]) bundle:nil] forCellReuseIdentifier:[NewCityHomeCell identifierStr]];
    
    
    
    
    
    MJWeakSelf
    self.plainTableView.mj_header = [XMGRefreshHeader headerWithRefreshingBlock:^{
        
        [weakSelf loadData];
        
    }];
    
    [weakSelf.plainTableView.mj_header beginRefreshing];
    
    
    
    
    
    self.plainTableView.mj_footer = [XMGRefreshFooter footerWithRefreshingBlock:^{
        
        [weakSelf loadMoreData];
        
    }];
    
    

}




- (void)loadData{
    self.curpage = 1;
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"gc_id"] = @"633";
    parameters[@"curpage"] = @"1";
    
    [[NetworkTools shareNetworkTools] sendRequestWithHttpMethod:GET_METHOD URLPath:@"mobile/index.php?c=goods&a=goods_list" parameters:parameters completionHandler:^(id responseObject, NSError *error) {
        
        if (!error) {
            //            NSLog(@"%@",[responseObject mj_JSONString]);
            
            
            self.hasmore = responseObject[@"data"][@"has_more"];
            
//            self.dynamicModels = [OneGoodsModel mj_objectArrayWithKeyValuesArray:responseObject[@"datas"][@"goods_list"]];
            
            [self.plainTableView.mj_footer resetNoMoreData];
            
            if ([self.hasmore isEqualToString:@"1"]) {
                
            } else {
                
                if (self.dynamicModels.count > 0) {
                    
                    [self.plainTableView.mj_footer endRefreshingWithNoMoreData];
                    
                } else {
                    
                    [SVProgressHUD showInfoWithStatus:@"暂无此类商品!"];
                    
                }
            }
            
            [self.plainTableView.mj_header endRefreshing];
            
            [self.plainTableView reloadData];
            
            
        }else{
            
            [self.plainTableView.mj_header endRefreshing];
            
            [SVProgressHUD showInfoWithStatus:error.domain];
        }
        
    }];
    
}



- (void)loadMoreData{
    
    self.curpage ++;
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"gc_id"] = @"633";
    parameters[@"curpage"] = [NSString stringWithFormat:@"%ld",(long)self.curpage];
    
    [[NetworkTools shareNetworkTools] sendRequestWithHttpMethod:GET_METHOD URLPath:@"mobile/index.php?c=goods&a=goods_list" parameters:parameters completionHandler:^(id responseObject, NSError *error) {
        
        if (!error) {
            
            self.hasmore = responseObject[@"data"][@"has_more"];
            
            [self.dynamicModels addObjectsFromArray:[OneGoodsModel mj_objectArrayWithKeyValuesArray:responseObject[@"datas"][@"goods_list"]]];
            
            
            if ([self.hasmore isEqualToString:@"1"]) {
                
                [self.plainTableView.mj_footer endRefreshing];
                
            } else {
                
                
                
                [self.plainTableView.mj_footer endRefreshingWithNoMoreData];
                
                
            }
            
            
            [self.plainTableView reloadData];
            
        }else{
            
            [self.plainTableView.mj_footer endRefreshing];
            
            [SVProgressHUD showInfoWithStatus:error.domain];
            
        }
        
    }];
    
}



#pragma mark - tableviewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    if (self.dynamicModels == nil) {
        return  0;
    } else {
        return  1;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    if (self.dynamicModels == nil) {
        
        return  0;
        
    } else {
        
        return 20;
//        return  self.dynamicModels.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NewCityHomeCell *tableCell = [tableView dequeueReusableCellWithIdentifier:[NewCityHomeCell identifierStr]];
    
    tableCell.model = self.dynamicModels[0];
    
    return tableCell;
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NewCityDynamicModel *model = self.dynamicModels[0];
    MyWebViewController *tempVC = [[MyWebViewController alloc]init];
    tempVC.urlString = model.dynamicDetailUrl;
    
    [self.navigationController pushViewController:tempVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




@end
